package com.bookshop.controller;

import com.bookshop.entity.*;
import com.bookshop.entity.response.R;
import com.bookshop.service.OrderService;
import com.bookshop.service.UserService;
import com.bookshop.util.GetIdUtil;
import com.bookshop.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * (Order)表控制层
 *
 * @author hk
 * @since 2024-01-17 09:41:05
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private GetIdUtil getIdUtil;
    @Resource
    private OrderService orderService;
    @Autowired
    public OrderController(OrderService orderService)
    {
        this.orderService = orderService;
    }

    @PostMapping("addToOrder")
    public R addToOrder(@RequestBody List<BookWithNum> books , HttpServletRequest request) throws SchedulerException {
        Integer id = getIdUtil.getId(request);
        System.out.println(id);

        Map<String , String> resultMap = null;

        resultMap = orderService.addToOrder(books , id);
        BookList bookList = orderService.getOrderList(Integer.parseInt(resultMap.get("orderId")) , 1 , 5);
        R r = R.ok();
        r.put("result" , resultMap);
        r.put("bookList" , bookList);

        return r;
    }
    @PostMapping("deleteOrder")
    public R deleteOrder(@RequestParam("orderNo") String orderNo , HttpServletRequest request)
    {
        int result = orderService.deleteOrder(orderNo);
        if(result == 0)
        {
            return R.error().message("删除失败!");
        }

        return R.ok().message("删除成功!");
    }
    @PostMapping("updatePayStatus")
    public R updatePayStatus(@RequestBody Map<String , String> requestMap)
    {
        int result = orderService.updatePayStatus(requestMap.get("orderNo") , Integer.parseInt(requestMap.get("payStatus")));
        if(result == 0)
        {
            return R.error().message("支付失败!");
        }
        else if(result == 2)
        {
            return R.error().message("订单超时！");
        }
        return R.ok().message("支付成功!");
    }
    @PostMapping("SearchByStatus")
    public R SearchByStatus(int payStatus , HttpServletRequest request)
    {
        Integer id = getIdUtil.getId(request);

        List<Order> orderList = orderService.searchByStatus(payStatus , id);

        R r = R.ok();
        r.put("OrderList" , orderList);
        return r;
    }
    //获取订单中所有图书的信息
    @PostMapping("getOrderList")
    public R getOrderList(@RequestBody Map<String , Integer> request)
    {
        Order order = orderService.getOrderInfo(request.get("orderId"));
        BookList bookList = orderService.getOrderList(request.get("orderId") , 1 , 50);
        if(bookList == null || order == null)
        {
            return R.error().message("订单查询失败");
        }
        R r = R.ok();
        r.put("orderInfo" , order);
        r.put("bookList" , bookList);
        return r;
    }
    //获取用户所有订单的信息
    @GetMapping("getAllOrder")
    public R getAllOrder(HttpServletRequest request)
    {

        int userId = getIdUtil.getId(request);
        OrderList orderList = orderService.getAllOrder(userId , 1 , 50);

        return R.ok().put("orderList" , orderList);
    }

}
















































































